package com.shanling.shanlingcontroller.ui.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shanling.shanlingcontroller.CustomApplication;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.CommonAdapter;
import com.shanling.shanlingcontroller.adapter.ViewHolder;
import com.shanling.shanlingcontroller.base.BaseLazyFragment;
import com.shanling.shanlingcontroller.base.BaseMVPFragment;
import com.shanling.shanlingcontroller.bean.Artist;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.FolderInfo;
import com.shanling.shanlingcontroller.persenter.FoldersPersenter;
import com.shanling.shanlingcontroller.persenter.contract.FoldersContract;
import com.shanling.shanlingcontroller.ui.activity.MusicListActivity;
import com.shanling.shanlingcontroller.ui.activity.MusicPlayActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ch1 on 2019/11/22.
 */

public class MusicFolderFragment extends BaseMVPFragment<FoldersContract.Presenter> implements FoldersContract.View, SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemClickListener {
    @BindView(R.id.lv_folder)
    ListView lvFolder;
    @BindView(R.id.sw)
    SwipeRefreshLayout sw;
    @BindView(R.id.bt_scan)
    Button btScan;
    @BindView(R.id.ll_nomusic)
    LinearLayout llNomusic;
    @BindView(R.id.pb)
    ProgressBar pb;
    private CommonAdapter<FolderInfo> myAdapter;
    private List<FolderInfo> folderInfos= new ArrayList<>();
    private boolean isRefresh = false;
    @Override
    protected void onLazyLoad() {
        isRefresh=false;
        mPresenter.loadFolders(CustomApplication.getInstance());
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_musicfolder;
    }

    @Override
    protected void initViewsAndEvents() {
        sw.setColorSchemeColors(0xffC8B492, 0xFF444444);
        sw.setOnRefreshListener(this);
        myAdapter = new CommonAdapter<FolderInfo>(getActivity(), folderInfos, R.layout.item_folder) {
            @Override
            public void convert(ViewHolder helper, int position, FolderInfo item) {
                TextView tv_name=helper.getView(R.id.tv_foldername);
                TextView tv_path=helper.getView(R.id.tv_path);
                if(!TextUtils.isEmpty(item.getFolderName())){
                    tv_name.setText(item.getFolderName());
                }else {
                    tv_name.setText(R.string.unknown);
                }
                if(!TextUtils.isEmpty(item.getFolderPath())){
                    tv_path.setText(item.getFolderPath());
                }else {
                    tv_path.setText(R.string.unknown);
                }
            }
        };
        lvFolder.setAdapter(myAdapter);
        lvFolder.setOnItemClickListener(this);


    }
    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }

    @OnClick(R.id.bt_scan)
    public void onViewClicked() {

    }

    @Override
    public void showLoading() {
        if (!isRefresh){
            pb.setVisibility(View.VISIBLE);
        }
        llNomusic.setVisibility(View.INVISIBLE);

    }

    @Override
    public void hideLoading() {
        pb.setVisibility(View.GONE);
        sw.setRefreshing(false);
    }

    @Override
    public void showEmptyView() {
        llNomusic.setVisibility(View.VISIBLE);
        pb.setVisibility(View.GONE);
        sw.setVisibility(View.INVISIBLE);
        sw.setRefreshing(false);

    }
    @Override
    public void showFolders(List<FolderInfo> infos) {
        llNomusic.setVisibility(View.INVISIBLE);
        sw.setRefreshing(false);
        pb.setVisibility(View.GONE);
        folderInfos.clear();
        folderInfos.addAll(infos);
        myAdapter.refresh(folderInfos);
    }
    @Override
    protected FoldersContract.Presenter createPresenter() {
        return new FoldersPersenter();
    }
    @Override
    public void onRefresh() {
        isRefresh=true;
        mPresenter.loadFolders(getActivity());
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle bundle=new Bundle();
        bundle.putString("path",folderInfos.get(position).getFolderPath());
        readyGo(MusicListActivity.class,bundle);
    }
}
